package com.cmccgz.udacs_landlord_webapp.main.peoplemanager;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.CardStateChangedAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.CardDetail;
import com.cmccgz.udacs_landlord_webapp.bean.CardMsg;
import com.cmccgz.udacs_landlord_webapp.bean.RenterBean;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.contract.RecoverCardContract;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.presenter.RecoverCardPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author INX
 *         恢复卡片
 */
public class RecoverCardActivity extends BaseActivity implements View.OnClickListener, CardStateChangedAdapter.OnCardStateChangeListenter,RecoverCardContract.View {
    private ViewHelper mViewHelper;
    private TextView tvName, tvNumber, tvTime;
    private RelativeLayout rlTime;
    private RecyclerView rv_cards;
    private List<CardDetail> datas=new ArrayList<>();
    private List<CardMsg.CardAllType>types=new ArrayList<>();
    private CardStateChangedAdapter adapter;
    private RecoverCardPresenter presenter;
    private RenterBean renterBean;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_stop_card;
    }

    @Override
    protected void initView() {
        tvName = (TextView) findViewById(R.id.tv_name);
        tvNumber = (TextView) findViewById(R.id.tv_number);
        tvTime = (TextView) findViewById(R.id.tv_time);
        rlTime = (RelativeLayout) findViewById(R.id.rl_time);
        rv_cards = ((RecyclerView) findViewById(R.id.rv_cards));
        rv_cards.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        adapter = new CardStateChangedAdapter(types,datas,mContext,this);
        rv_cards.setAdapter(adapter);
        ((TextView) findViewById(R.id.tv_cards_title)).setText("目前暂停");
        initDatas();
        initBaseDataForView();
        presenter = RecoverCardPresenter.instance(this);
        getCards();
    }

    private void getCards() {
        showLoadingDialog("正在获取已暂停的卡片列表...");
        presenter.getStopedCardMsg(renterBean.user_mobile,renterBean.house_id);
    }

    private void initBaseDataForView() {
        if (renterBean==null) {
            return;
        }
        tvName.setText(renterBean.user_name);
        tvNumber.setText(renterBean.house_name);
        tvTime.setText(renterBean.house_end_time);
    }

    private void initDatas() {
        renterBean = (RenterBean) getIntent().getSerializableExtra("renterBean");
        if (renterBean==null) {
            showToast("获取信息失败！");
            finish();
        }
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {
        mViewHelper = ViewHelper.instance(mContext);
        if (mViewHelper != null) {
            mViewHelper.setTitle("恢复卡片");
            mViewHelper.setBackTextVisable(true);
            mViewHelper.setRightText("完成");
            mViewHelper.setTextRightVisable(true);
        }

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    public void onClickEmptyView() {
        getCards();
    }

    @Override
    public void recoverSuccess() {
        dismissLoadingDialog();
        finish();
    }

    @Override
    public void recoverFail(String msg) {
        dismissLoadingDialog();
        showToast(msg);
    }

    @Override
    public void getCardMsgSuccess(CardMsg cardMsg) {
        datas.clear();
        if (cardMsg!=null) {
            if (cardMsg.card_detail!=null) {
                datas.addAll(cardMsg.card_detail);
            }
            if (cardMsg.card_all_type!=null) {
                types.addAll(cardMsg.card_all_type);
            }
        }
        adapter.updateTypeAndNotify(types);
        dismissLoadingDialog();
    }

    @Override
    public void getCardMsgFail(String msg) {
        showToast(msg);
        dismissLoadingDialog();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_right:
                showLoadingDialog("正在恢复...");
                presenter.recoverCard(renterBean.user_mobile,renterBean.house_id,datas);
                break;
        }
    }
}
